import {
	call_kw,
} from "./apibase.js";
import store from "@/store";
// 添加客户
export const customerAdd = (params) => {
	return call_kw({
		model: 'res.partner',
		method: 'create',
		args: [{
			"name": params.name,
			"mobile": params.mobile,
			"company_id": params.company_id,
			"function": params.function,
			"image_1920": params.image_1920,
			"is_company": true
		}],
		kwargs: {},
	});
}

// 编辑客户
export const customerEdit = (params) => {
	return call_kw({
		model: 'res.partner',
		method: 'update',
		args: [
			[params.id],
			{
				"name": params.name,
				"mobile": params.mobile,
				"company_id": params.company_id,
				"function": params.function,
				"image_1920": params.image_1920,
				"is_company": true
			}
		],
		kwargs: {},
	});
}

// 编辑用户信息
export const personEdit = (params) => {
	return call_kw({
		model: 'hr.employee',
		method: 'update',
		args: [
			[Number(params.id)],
			{
				"name": params.name,
				"mobile_phone": params.phone,
				"image_1920": params.image_1920,
			}
		],
		kwargs: {},
	});
}

// 修改密码
export const passwordUpdate = (params) => {
	let user_context = store.state.user_context;
	user_context = JSON.parse(user_context || '{}');
	return call_kw({
		model: 'res.users',
		method: 'change_password',
		args: [
			params.old_passwd,
			params.new_passwd,
		],
		kwargs: {},
	});
}

// 预览报价
export const quotationPreview = (params) => {
	return call_kw({
		model: 'sale.quotation',
		method: 'button_show_quotation_data',
		args: [
			Number(params.id)
		],
		kwargs: {},
	});
}

// 报价
export const quotationCreate = (params) => {
	return call_kw({
		model: 'sale.quotation',
		method: 'button_confirmed',
		args: [
			Number(params.id)
		],
		kwargs: {},
	});
}

// 产品必填的字段
export const productFields = (params) => {
	return call_kw({
		model: 'finished.product',
		method: "model_new_sale_quotation",
		args: [
			Number(params.id),
		],
		kwargs: {},
	});
}

// 获取商机记录
export const previewRecord = (params) => {
	return call_kw({
		model: 'finished.product',
		method: "model_show_sale_quotation",
		args: [
			Number(params.id),
		],
		kwargs: {},
	});
}

// 点击预报价按钮
export const quotationCompute = (params) => {
	let obj = {
		...params.order
	};
	if (params.lines?.length > 0) {
		obj.line_ids = [];
		params.lines.forEach(item => {
			obj.line_ids.push([
				1,
				Number(item.id),
				{
					product_qty: Number(item.product_qty)
				}
			])
		})
	}
	return call_kw({
		model: 'sale.quotation',
		method: "api_button_compute",
		args: [
			Number(params.id),
			obj,
		],
		kwargs: {},
	});
}

// 报价记录详情
export const getConfirmedQuotationData = (params) => {
	return call_kw({
		model: 'sale.quotation',
		method: "get_confirmed_quotation_data",
		args: [
			Number(params.id),
		],
		kwargs: {},
	});
}